//
//  InfoScene.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 10/15/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "RequirePopup.h"
#import "MenuScene.h"
#import "LevelManager.h"
#import "DefineText.h"
#import "Utility.h"

@implementation RequirePopup

-(id) init
{
    if (self = [super initWithColor:ccc4(0, 0, 0, 0)])
    {
        CCLOG(@"require init");
        [self initMenu];
        
        self.touchEnabled = YES;
        
        CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:POPUP_ANIMATE_TIME opacity:POPUP_ALPHA];
        [self runAction:fadeTo];
        
        for (int i = 0; i < _children.count; i++)
        {
            CCFadeTo *f = [CCFadeTo actionWithDuration:1 opacity:255];
            CCSprite *node = [_children objectAtIndex:i];
            [node setOpacity:0];
            [node runAction:f];
        }
    }
    
    return self;
}

-(void) registerWithTouchDispatcher
{
    [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:TOUCH_PRIORITY_RESULT_POPUP swallowsTouches:YES];
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    return YES;
}

-(void) initMenu
{
    mMenu = [[CCMenu alloc] init];
    [mMenu setPosition:ccp(0, 0)];
    [self addChild:mMenu];
    
    [mMenu setTouchPriority:TOUCH_PRIORITY_RESULT_POPUP];
    
    
    // create panel
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    CCSprite *panel = [CCSprite spriteWithSpriteFrameName:SFN_PANEL_1];
    [panel setPosition:ccp(winSize.width / 2, winSize.height / 2 + uniPos(50, 100))];
    [panel setColor:CL_PANEL_BG];
    [self addChild:panel];
    
    // create button main menu
    CCSprite *sprMainMenu = [CCSprite spriteWithSpriteFrameName:SFN_PANEL_BTN_4_3];
    CCSprite *sprMainMenuHover = [CCSprite spriteWithSpriteFrameName:SFN_PANEL_BTN_4_3];
    [sprMainMenu setColor:CL_PANEL_BG];
    [sprMainMenuHover setColor:CL_PANEL_BTN];
    CCMenuItem *miOK = [CCMenuItemSprite itemWithNormalSprite:sprMainMenu
                                               selectedSprite:sprMainMenuHover
                                                       target:self
                                                     selector:@selector(onOK)];
    [miOK setPosition:ccp(winSize.width / 2, panel.position.y - uniPos(100.5, 201))];
    [mMenu addChild:miOK];
    
    CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"Tap here to continue" fntFile:FONT_DEFAULT];
    [lb setAnchorPoint:ccp(0.5, 0.5)];
    [lb setPosition:miOK.position];
    [self addChild:lb];
    
    CCLabelBMFont *lb2 = [CCLabelBMFont labelWithString:@"Requirement" fntFile:FONT_DEFAULT];
    [lb2 setAnchorPoint:ccp(0.5, 0.5)];
    [lb2 setPosition:ccp(winSize.width / 2, winSize.height / 2 + uniPos(95, 190))];
    [self addChild:lb2];
    
    [self initRequire];
}

-(void) onOK
{
    [[Utility getInstance] playEffect:SOUND_MENU_BUTTON];
    for (int i = 0; i < _children.count; i++)
    {
        CCSprite *node = [_children objectAtIndex:i];
        [node stopAllActions];
    }
    
    CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:POPUP_ANIMATE_TIME opacity:0];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self stopAllActions];
        [self removeFromParent];
        [[NSNotificationCenter defaultCenter] postNotificationName:EVENT_REQUIRE_POPUP_CLOSE object:nil];
    }];
    CCSequence *sequence = [CCSequence actions:fadeTo, callBlock, nil];
    [self runAction:sequence];
    
    for (int i = 0; i < _children.count; i++)
    {
        CCFadeTo *f = [CCFadeTo actionWithDuration:0.3 opacity:0];
        CCSprite *node = [_children objectAtIndex:i];
        [node runAction:f];
    }
}

-(void) initRequire
{
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    float startX = (winSize.width / 2) - uniPos(100, 200);
    float startY = (winSize.height / 2) + uniPos(50, 100);
    
    NSArray *listRequire = [[LevelManager getInstance] getListLevelRequire];
    for (int i = 0; i < listRequire.count; i++)
    {
        LevelRequire *require = [listRequire objectAtIndex:i];
        CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"" fntFile:FONT_DEFAULT];
        [lb setString:[self getStringRequire:require]];
        [lb setAnchorPoint:ccp(0, 0.5)];
        [lb setPosition:ccp(startX, startY)];
        [self addChild:lb];
        
        CCSprite *spr = [CCSprite spriteWithSpriteFrameName:SFN_ICON_STAR_SMALL];
        [spr setPosition:ccp(startX - uniPos(20, 40), startY)];
        [self addChild:spr z:0];
        
        startY -= uniPos(30, 60);
    }
}

-(NSString *) getStringRequire:(LevelRequire *)require
{
    NSString *str;
    if (require._type == eLRT_FINISH_PATH)
    {
        str = [NSString stringWithFormat:@"%@", TEXT_LEVEL_REQUIRE_FINISH_PATH];
        return str;
    }
    else if (require._type == eLRT_RUSH_TIME)
    {
        if (require._value > 1)
            str = [NSString stringWithFormat:TEXT_LEVEL_REQUIRE_RUSH_TIMES, require._value];
        else
            str = [NSString stringWithFormat:TEXT_LEVEL_REQUIRE_RUSH_TIME, require._value];
        return str;
    }
    else if (require._type == eLRT_ENERGIZE)
    {
        if (require._value > 1)
            str = [NSString stringWithFormat:TEXT_LEVEL_REQUIRE_ENERGIZES, require._value];
        else
            str = [NSString stringWithFormat:TEXT_LEVEL_REQUIRE_ENERGIZE, require._value];
        return str;
    }
    
    return nil;
}


-(void) dealloc
{
    CCLOG(@"Require popup dealloc!");
    [super dealloc];
}

@end
